<!--
@author  tanyuze
@desc    打卡活动
@date    2022/11/10
-->
<template>
  <div id="hundredLearningIndex">
    <el-collapse-transition>
      <div v-show="searchShow" class="common-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input v-model="listQuery.name" clearable placeholder="按活动名称进行搜索" class="search-input"
          @keyup.enter.native="handleFilter" />
        <div class="common-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">{{ $t('table.search') }}</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
          <span class="common-search-more" @click="searchShow = !searchShow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>
        </div>
      </div>
    </el-collapse-transition>
    <el-collapse-transition>
      <div v-show="!searchShow" class="common-search separate">
        <div class="common-search-inner">
          <el-row :gutter="20">
            <el-col :span="12">
              <div class="search-inner-item">
                <div class="item-label">活动名称</div>
                <el-input v-model="search.name" clearable class="filter-item" style="width: 100%;"
                  placeholder="按活动名称进行搜索" />
              </div>
            </el-col>
            <el-col :span="12">
              <div class="search-inner-item">
                <div class="item-label">资源类型</div>
                <el-select v-model="search.clockResourceType" placeholder="请选择" style="width: 100%">
                  <el-option v-for="(item, index) in dict.DKZYLX" :key="index" :label="item.dictName"
                    :value="item.dictValue" />
                </el-select>
              </div>
            </el-col>
          </el-row>
        </div>
        <div class="common-search-btn">
          <el-button type="primary" @click="handleFilter">{{ $t('table.search') }}</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
          <span class="common-search-more" @click="searchShow = !searchShow">
            精简筛选条件
            <i class="el-icon-arrow-up" />
          </span>
        </div>
      </div>
    </el-collapse-transition>

    <div class="btn-container">
      <div class="btn-title">
        <span>打卡活动管理</span>
      </div>
      <div class="btn-btns">
        <el-button v-waves style="margin-left: 10px;" @click="handleCreate">新建</el-button>
        <el-button style="margin-left: 10px;" @click="handleSelectPlatform">选取SaaS平台打卡活动</el-button>
        <el-button v-waves style="margin-left: 10px;" @click="handleDeletes">批量删除</el-button>
      </div>
    </div>

    <div class="table">
      <v-table :list="list" tag="clockList" :list-loading="listLoading" :filter-column="filterColumn" :selection="true"
        :source-arr="sourceArr" @handleSelectionChange="handleSelectionChange">
        <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleView(scope.row)">预览</el-button>
            <el-button v-if="scope.row.isForce !== 'SF01'" type="text" size="mini" @click="handleUpdateState(scope.row)">
              {{ scope.row.status == 'ZT01' ? '禁用' : '启用' }}
            </el-button>
            <el-button v-if="scope.row.isForce !== 'SF01'" type="text" size="mini"
              @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button v-if="scope.row.isForce !== 'SF01'" type="text" size="mini"
              @click="handleDel(scope.row)">删除</el-button>
            <el-button v-if="scope.row.isForce !== 'SF01'" type="text" size="mini"
              @click="handleResource(scope.row)">资源</el-button>
            <el-button type="text" size="mini" @click="handleConfig(scope.row)">配置</el-button>
            <el-button type="text" size="mini" @click="handleStat(scope.row)">统计</el-button>
            <el-button type="text" size="mini" @click="handleRanking(scope.row)">排行榜</el-button>
            <el-button v-if="scope.row.isForce === 'SF01'" type="text" size="mini"
              @click="handleSort(scope.row)">修改排序</el-button>
          </template>
        </el-table-column>
      </v-table>
    </div>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />
    <iframe-preview v-if="previewStatus" :src="previewPath" @closeSet="previewStatus = false" />
    <el-dialog title="修改排序" width="30%" :visible.sync="sortDialog" @close="cancelDialog" v-el-drag-dialog>
      <el-input v-model="form.sort" placeholder="请输入排序值" type="number"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button style="padding: 8px 30px;" @click="cancelDialog">取 消</el-button>
        <el-button style="padding: 8px 30px;" type="primary" @click="handelUpdateSort">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { queryPage, del, upStatus, getDictByCode, update } from '../api'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import VTable from '@/components/Table'
import IframePreview from '@/views/activity/tphd/components/iframePreview'
import elDragDialog from '@/directive/el-drag-dialog'
export default {
  name: 'PunchClockIndex',
  components: { Pagination, VTable, IframePreview },
  directives: { waves, elDragDialog },
  data() {
    return {
      searchShow: true,
      search: {
        name: '',
        clockResourceType: ''
      },
      listQuery: {
        page: 1,
        limit: 10,
        name: ''
      },
      list: [],
      total: 0,
      sourceArr: ['coverImgIdURL'],
      listLoading: false,
      multipleSelection: [],
      dict: {
        DKZYLX: []
      },
      previewStatus: false,
      obj: {},
      previewPath: '',
      form: {
        id: '',
        sort: ''
      },
      sortDialog: false
    }
  },
  created() {
    this.getList()
    this.getDict('DKZYLX')
  },
  methods: {
    handleSelectPlatform() {
      this.$router.push({ path: '/activity/punchClock/SelectCourse4platform', query: {} })
    },
    getDict(code) {
      return new Promise((resolve, reject) => {
        getDictByCode(code)
          .then((response) => {
            this.dict[code] = response.data.data[0].list
            resolve()
          })
          .catch((error) => {
            reject(error)
          })
      })
    },
    filterColumn(scope, item) {
      if (item === 'status') {
        return scope.row[item] === 'ZT02' ? '禁用' : '启用'
      }
      return scope.row[item]
    },
    handleSelectionChange(arr) {
      this.multipleSelection = arr
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resettingBtn() {
      this.listQuery.name = ''
      for (const i in this.search) {
        this.search['' + i + ''] = ''
      }
      this.listQuery.page = 1
      this.getList()
    },
    handleUpdateState(row) {
      const status = row.status === 'ZT01' ? 'ZT02' : 'ZT01'
      const desc = row.status === 'ZT01' ? '禁用' : '启用'
      this.$confirm(`确认要${desc}该活动吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          upStatus({ id: row.id, status: status })
            .then((res) => {
              loading.close()
              this.$message({
                title: '成功',
                message: desc + '成功',
                type: 'success',
                duration: 2000
              })
              this.getList()
            })
            .catch(() => {
              loading.close()
            })
        })
        .catch(() => {
        })
    },
    handleCreate() {
      this.$router.push({
        path: this.$filterPath('edit'),
        query: { type: 'c' }
      })
    },
    handleUpdate(item) {
      this.$router.push({
        path: this.$filterPath('edit'),
        query: {
          type: 'u',
          i: item.id
        }
      })
    },
    /**
     * 预览
     */
    handleView(row) {
      this.obj = Object.assign({}, row)
      this.previewStatus = true
      if (process.env.NODE_ENV === 'development') {
        this.previewPath = `http://192.168.105.47:8889/#/punchClock?i=${row.id}&origin=admin`
      } else {
        this.previewPath = location.origin + `/#/punchClock?i=${row.id}&origin=admin`
      }
    },
    handleResource(row) {
      this.$router.push({
        path: this.$filterPath('clockResource'),
        query: {
          i: row.id,
          type: row.clockResourceType
        }
      })
    },
    handleConfig(row) {
      this.$router.push({
        path: this.$filterPath('clockConfiguration'),
        query: {
          i: row.id,
          type: row.clockResourceType
        }
      })
    },
    handleStat(row) {
      this.$router.push({
        path: this.$filterPath('sign'),
        query: {
          id: row.id,
          title: row.name,
          type: row.clockResourceType
        }
      })
    },
    handleDel(row) {
      this.deleteItem(row.id)
    },

    // 进入排行榜
    handleRanking(row) {
      this.$router.push({ path: this.$filterPath('punchRanking'), query: { i: row.id } })
    },

    handleDeletes() {
      const arr = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item) => {
          arr.push(item.id)
        })
        var ids = arr.join(',')
        this.deleteItem(ids)
      } else {
        this.$message({
          message: '请选择需要删除的数据！',
          type: 'warning'
        })
      }
    },
    deleteItem(ids) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: '请稍后',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.3)'
          })
          return new Promise((resolve, reject) => {
            del({ ids: ids })
              .then(() => {
                loading.close()
                this.$message({
                  title: '成功',
                  message: '删除成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList()
              })
              .catch((error) => {
                loading.close()
                reject(error)
              })
          })
        })
        .catch((e) => {
        })
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        name: this.listQuery.name
      }
      if (!this.searchShow) {
        query['name'] = this.search.name
        query['clockResourceType'] = this.search.clockResourceType
      }
      return new Promise((resolve, reject) => {
        queryPage(query)
          .then((res) => {
            this.list = res.data.data.rows
            this.total = res.data.data.total
            this.listLoading = false
          })
          .catch((error) => {
            reject(error)
            this.listLoading = false
          })
      })
    },

    // 修改排序
    handelUpdateSort() {
      if (this.form.sort === '') {
        this.$message({
          message: '请输入排序值',
          type: 'warning'
        })
        return
      }
      const obj = Object.assign({}, this.form)
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.3)'
      })
      update(obj).then(res => {
        if (res.data.code === '200') {
          loading.close()
          this.sortDialog = false
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getList()
        } else {
          loading.close()
          this.$message({
            message: '修改失败',
            type: 'error'
          })
        }
      }).catch(() => {
        loading.close()
      })
    },

    // 强制下发数组修改排序
    handleSort(row) {
      console.log(row, '482');
      this.form.id = row.id
      this.sortDialog = true
    },

    // 取消修改排序
    cancelDialog() {
      this.sortDialog = false
      for (let k in this.form) {
        this.form[k] = ''
      }
    }
  }
}
</script>

<style lang="scss">
$dark_gray: #bfced6;

#hundredLearningIndex {
  padding: 20px;

  .common-search {
    background-color: #fff;
    padding: 0 20px;

    .svg-container {
      padding: 0;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;

      &_login {
        font-size: 20px;
      }
    }

    .search-input {
      width: 60%;
      position: relative;

      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }

      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;

        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }

    .common-search-btn {
      display: inline-block;
      float: right;

      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }

      .common-search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .common-search-inner {
        padding-top: 10px;

        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }

          .el-input {
            position: relative;

            &.el-date-editor .el-input__inner {
              padding-left: 30px;
            }

            .el-input__inner {
              border: none;
              padding: 0;
            }

            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }

      .common-search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .btn-container {
    display: flex;
    height: 36px;
    line-height: 36px;
    margin-top: 20px;
    margin-bottom: 20px;

    .btn-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      font-family: PingFangSC-Medium;
      color: rgba(51, 51, 51, 1);
    }
  }

  .dataForm::-webkit-scrollbar {
    display: none;
  }

  .avatar-uploader {
    cursor: pointer;
    position: relative;
    overflow: hidden;
    background: rgba(247, 247, 248, 1);
    border: 1px solid rgba(226, 227, 232, 1);
    border-radius: 4px;
    width: 300px;
    height: 180px;

    &:hover {
      border-color: #fe5a61;
    }

    img {
      width: 100%;
      height: 100%;
    }

    .avatar-uploader-icon {
      font-size: 48px;
      color: #dadada;
      width: 300px;
      height: 180px;
      line-height: 180px;
      text-align: center;
    }
  }
}
</style>
